const data = require("./mock.json");
/**
 * 利用队列和广度遍历
 * @param {node} tree 树节点
 * @returns Array
 */
const tree2Array = (tree) => {
  const res = [],queue = [tree];
  while (queue.length) {
    const item = queue.shift();
    res.push({
      id: item.id,
      parentId: item.pid,
      name: item.lable,
    });
    let child = item.child;
    if (child) {
      child.forEach((c) => {
        queue.push(c);
      });
    }
  }
  return res;
};
console.log(data);
console.log(JSON.stringify(tree2Array(data[0])));


const tree2Array2 = (node) => {
  let queue = [node],
    res = [];
  while (queue.length) {
    const item = queue.shift();
    res.push({
      id: item.id,
      name: id.lable,
      pid: id.parentId,
    });
    if (item.child) {
      item.child.forEach((c) => {
        queue.push(c);
      });
    }
    return res;
  }
};


function tree2Arr(node){
  let queue=[node],res=[]
  while(queue.length){
    const item = queue.shift();
    res.push({
      id:item.id,
      name:item.name,
      pid:item.pid
    })

    const child = item.child||[]
    if(child){
      child.forEach(c=>{



        queue.push(c)
      })
    }
  }
  return res;
}


function tree2Arr(node){
  const queue=[node],res =[]

  while (queue.length){
    const item = queue.shift()
    res.push({
      id:item.id,
      label:item.label
    })
    const child = item.child
    if(child&&child.length){
      child.forEach(c=>{
        queue.push(c)
      })
    }
  }
  return res;
}



function tree2Arr(node) {
  const queue = [node],
    res = [];

  while (queue.length) {
    const cur = queue.shift();

    res.push({
      id: cur.id,
      name: cur.id,
    });
    if (cur.child && cur.child.length) {
      cur.child.forEach((item) => {
        queue.push(item);
      });
    }
  }
  return res;
}


function tree2Array3(node) {
  const queue = [node],
    res = [];

  while (queue.length) {
    const cur = queue.shift();

    res.push({
      id: cur.id,
      name: cur.name,
    });
    const child = cur.child;
    if (child) {
      child.forEach((c) => {
        queue.push(c);
      });
    }
  }
  return res;
}